﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Aribaccio.Device
{
    public class AsciiSlaveData : AsciiSlave
    {
        /// <summary>
        /// 数据字符串
        /// </summary>
        public string Data { get; set; }
        /// <summary>
        /// 数据字节集
        /// </summary>
        public byte[]? DataBytes { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public AsciiSlaveData()
            : base()
        {
            this.Data = string.Empty;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public AsciiSlaveData(AsciiSlave argSlave) : base()
        {
            this.Data = string.Empty;
            if (argSlave != null)
            {
                this.Command = argSlave.Command;
                this.IsWaitResult = argSlave.IsWaitResult;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public AsciiSlaveData(string argCommand, bool argIsWaitResult = true)
            : base(argCommand, argIsWaitResult)
        {
            this.Data = string.Empty;
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="argData">ASCII字符串</param>
        public void UpdateData(string argData)
        {
            this.Data = argData.TrimEnd('\r', '\n'); ;
            this.DataBytes = this.Data.ToAsciiBytes();
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="argData">ASCII字符串</param>
        public void UpdateData(byte[] argDataBytes)
        {
            this.DataBytes = argDataBytes;
            this.Data = argDataBytes.ToAsciiString();
            if (string.IsNullOrEmpty(this.Data))
            {
                this.Data = this.Data.TrimEnd('\r', '\n');
            }
        }

        public override string ToString()
        {
            return string.Concat(this.Command, this.Data);
        }
    }
}
